package com.youkeda.comment.service.impl;

import com.youkeda.comment.dao.UserDAO;
import com.youkeda.comment.dataobject.UserDO;
import com.youkeda.comment.model.Result;
import com.youkeda.comment.model.User;
import com.youkeda.comment.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName UserServiceImpl
 * @Author 刘正星
 * @Date 2020/7/16 13:32
 **/
//让系统能够自动扫描（配置文件中dubbo.scan.base-packages 指定的包才会扫描）并识别这是一个Dubbo服务
@DubboService(version = "${user.service.version}")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDAO userDAO;

    @Override
    public Result<User> register(String userName, String pwd) {
        Result<User> result = new Result<>();
        //这段逻辑推荐只用if，if-else多层嵌套阅读性很差。
        //用户名为空
        if (StringUtils.isEmpty(userName)) {
            result.setCode("600");
            result.setMessage("用户名不能为空！");
        } else {
            //用户名不为空，密码为空
            if (StringUtils.isEmpty(pwd)) {
                result.setCode("601");
                result.setMessage("密码不能为空");
            } else {
                //用户名不为空，密码不为空
                //判断用户是否存在
                UserDO userDO = userDAO.findByUserName(userName);
                //用户不存在
                if (userDO == null) {
                    String saltPwd = pwd + "_lzx0203";
                    String md5Pwd = DigestUtils.md5Hex(saltPwd).toUpperCase();
                    userDO = new UserDO();
                    userDO.setUserName(userName);
                    userDO.setNickName(userName);
                    userDO.setPwd(md5Pwd);
                    //储存用户记录
                    userDAO.add(userDO);
                    result.setSuccess(true);
                    //将 UserDO 对象转化为 User 对象
                    User user = userDO.toModel();

                    result.setData(user);
                } else {
                    //用户名已经存在
                    result.setCode("602");
                    result.setMessage("用户名已经存在");
                }
            }
        }
        return result;
    }

    @Override
    public Result<User> login(String userName, String pwd) {

        Result<User> result = new Result<>();
        if (StringUtils.isEmpty(userName)) {
            result.setSuccess(false);
            result.setCode("600");
            result.setMessage("用户名不能为空");
            return result;
        }
        if (StringUtils.isEmpty(pwd)) {
            result.setCode("601");
            result.setSuccess(false);
            result.setMessage("密码不能为空");
            return result;
        }

        UserDO userDO = userDAO.findByUserName(userName);
        if (userDO == null) {
            result.setCode("602");
            result.setMessage("用户名不存在");
            return result;

        }
        String saltPwd = pwd + "_lzx0203";
        String md5Pwd = DigestUtils.md5Hex(saltPwd).toUpperCase();
        if (!md5Pwd.equals(userDO.getPwd())) {
            result.setCode("603");
            result.setMessage("密码不对");
            return result;
        }
        User user = userDO.toModel();

        result.setSuccess(true);
        result.setData(user);
        return result;
    }

    @Override
    public List<User> findAll() {

        List<User> users = userDAO.findAll().stream().map(userDO -> userDO.toModel()).collect(Collectors.toList());
        return users;
    }

    @Override
    public int add(User user) {
        if (user == null) {
            return 0;
        }
        UserDO userDO = new UserDO(user);
        int add = userDAO.add(userDO);
        if (add < 1) {
            return 0;
        }
        return add;
    }

    @Override
    public int update(User user) {
        if (user == null) {
            return 0;
        }
        int update = userDAO.update(new UserDO(user));
        if (update < 1) {
            return 0;
        }
        return update;
    }

    @Override
    public int delete(long id) {
        int delete = userDAO.delete(id);
        if (delete < 1) {
            return 0;
        }
        return delete;
    }

    @Override
    public User findByUserName(String name) {
        if (StringUtils.isEmpty(name)) {
            return null;
        }
        User user = userDAO.findByUserName(name).toModel();
        return user;
    }

    @Override
    public List<User> query(String keyWord) {
        if (StringUtils.isEmpty(keyWord)) {
            return null;
        }
        List<UserDO> userDOS = userDAO.query(keyWord);

        if (CollectionUtils.isEmpty(userDOS)) {
            return null;
        }
        List<User> users = userDOS.stream().map(userDO -> userDO.toModel()).collect(Collectors.toList());
        return users;
    }

    @Override
    public List<User> search(String keyWord, LocalDateTime startTime, LocalDateTime endTime) {
        if (StringUtils.isEmpty(keyWord) || startTime == null || endTime == null) {
            return null;
        }
        List<UserDO> userDOS = userDAO.search(keyWord, startTime, endTime);
        if (CollectionUtils.isEmpty(userDOS)) {
            return null;
        }
        List<User> users = userDOS.stream().map(userDO -> userDO.toModel()).collect(Collectors.toList());
        return users;
    }

    @Override
    public int batchAdd(List<User> users) {
        if (users == null || CollectionUtils.isEmpty(users)) {
            return 0;
        }
        List<UserDO> userDOS = users.stream().map(user -> {
            UserDO userDO = new UserDO(user);
            return userDO;
        }).collect(Collectors.toList());
        int batchAdd = userDAO.batchAdd(userDOS);
        if (batchAdd < 1){
            return 0;
        }
        return batchAdd;
    }

    @Override
    public List<User> findByIds(List<Long> ids) {
        if (ids == null || CollectionUtils.isEmpty(ids)){
            return null;
        }
        List<UserDO> userDOS = userDAO.findByIds(ids);
        if (CollectionUtils.isEmpty(userDOS)){
            return null;
        }
        List<User> users = userDOS.stream().map(userDO -> userDO.toModel()).collect(Collectors.toList());
        return users;
    }


}
